home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -serious- / programming / basic / mildred / lha / gravityexample.lha / GravityExample.ascii next >
Text File  |  1999-03-03  |  11KB  |  326 lines

  1. WBStartup
  2. NoCli
  3.  
  4. ; Map files full path
  5. map$="data/map.lmap"
  6.  
  7. #vwidth=320             ; ViewWidth
  8. #vwidth2=#vwidth/2      ; Half ViewWidth
  9. #vheight=256            ; ViewHeight
  10. #vheight2=#vheight/2    ; Half ViewHeight
  11. #bmwidth=#vwidth        ; Bitmap Width
  12. #bmheight=#vheight      ; Bitmap Height
  13. #numblocks=2            ; How Many Background Blocks we use
  14.  
  15. #numbul=200             ; number of available bullets
  16.  
  17. #anglesweep=64          ; Full Circle (have to be power of 2!!!)
  18. #half=#anglesweep/2     ; Half Circle
  19. #quarter=#half/2        ; Quarter Circle
  20. #quarquar=#quarter/2    ; Half Of Qaurter Circle
  21.  
  22. #BOOL_FALSE=0           ; BOOLEAN FALSE (same as False)
  23. #BOOL_TRUE=1            ; BOOLEAN TRUE  (same as True)
  24.  
  25. slot.w=10               ; slot^2 is the amount of full views in
  26.                         ; our playing arena
  27. degrad.q=Pi/(#anglesweep/2) ; Adding "Angle" by one adds it this many radians
  28. death.w=#BOOL_FALSE     ; Are we Dead?
  29.  
  30. NEWTYPE .ship           ; Our Ship
  31.   x.q
  32.   y.q
  33.   velx.q
  34.   vely.q
  35. End NEWTYPE
  36.  
  37. NEWTYPE.pos             ; Bullets Position
  38.   x.q
  39.   y.q
  40. End NEWTYPE
  41.  
  42. NEWTYPE.vel             ; Bullets Velocity
  43.   vx.q
  44.   vy.q
  45. End NEWTYPE
  46.  
  47. MReserveBitmaps 1    ; We're going to use 1 chunky bitmap.
  48. MReservec2pWindows 1 ; We only need one c2p display.
  49.                      ; We need shapes for the ship and for background.
  50.                      ; and one for background saving
  51. MReserveShapes #anglesweep+#numblocks+2
  52.  
  53. .initgraphics
  54. ; Here we will allocate our chunky buffer.
  55. dwidth.w=slot*#vwidth
  56. dheight.w=slot*#vheight
  57. cmap.l=MBitmap(0,dwidth,dheight) ; 10*10 times the view so it's huge ;)
  58. If cmap=0                        ; If not enough memory we try to get smaller one
  59.   slot=slot-2
  60.   If slot>0                      ; if we have slot value at least 1 we try to get the bitmap
  61.     Goto initgraphics
  62.   Else
  63.     End                          ; if it goes for null we have only one solution -> to QUIT
  64.   EndIf
  65. EndIf
  66.  
  67. MUseBitmap 0                     ; And We use it
  68.  
  69. ; Make some shapes
  70. MBoxF 0,0,15,15,127              ; This is normal Wall
  71. MGetaShape 1,0,0,16,16           ; Grab the shape
  72. MBoxF 0,0,15,5,255               ; And This one is a landing zone
  73. MGetaShape 2,0,0,16,16           ; And Grab it
  74. MBoxF 0,0,15,15,0                ; Clean the area
  75. Dim scx.q(#anglesweep-1,2)       ; These are used in collision detecting
  76. Dim scy.q(#anglesweep-1,2)       ; Very lame, but enough for example ;)
  77. Dim bx(#anglesweep-1)            ; Bullet start x offset
  78. Dim by(#anglesweep-1)            ; Bullet start y offset
  79. Dim bvx(#anglesweep-1)           ; Bullet X Velocity
  80. Dim bvy(#anglesweep-1)           ; bullet Y Velocity
  81. For l.l=0 To #anglesweep-1       ; Make The Ship and fill those collision
  82.                                  ; etc arrays
  83.   bx(l)=Cos((l+#half)*degrad)*10+7
  84.   by(l)=Sin((l+#half)*degrad)*10+7
  85.   bvx(l)=Cos((l+#half)*degrad)*4.5
  86.   bvy(l)=Sin((l+#half)*degrad)*4.5
  87.   x1.q=Cos((l+#half)*degrad)*7+7
  88.   y1.q=Sin((l+#half)*degrad)*7+7
  89.   x2.q=Cos((l-#quarquar)*degrad)*5+7
  90.   y2.q=Sin((l-#quarquar)*degrad)*5+7
  91.   x3.q=Cos((l+#quarquar)*degrad)*5+7
  92.   y3.q=Sin((l+#quarquar)*degrad)*5+7
  93.   scx(l,0)=x1
  94.   scy(l,0)=y1
  95.   scx(l,1)=x2
  96.   scy(l,1)=y2
  97.   scx(l,2)=x3
  98.   scy(l,2)=y3
  99.   MInk 255
  100.   MLine x1,y1,x2,y2
  101.   MLine x3,y3
  102.   MLine x1,y1
  103.   MGetaShape l+3,0,0,16,16
  104.   MBoxF 0,0,15,15,0
  105. Next l
  106. BitMap 2,200,160,2        ; Initialize our maps bitmap
  107. LoadBitMap 2,map$         ; We need 2 plane one
  108. Use BitMap 2
  109. For l.l=0 To slot*20-1
  110.   For t.l=0 To slot*16-1
  111.     ll.l=l LSL 4
  112.     tt.l=t LSL 4
  113.     s.w=Point(l,t)
  114.     If s>0
  115.       MTile16x16 s,ll,tt         ; We build up the scene
  116.     Else
  117.       MBoxF ll,tt,ll+15,tt+15,0  ; These are the FLY ZONE ;)
  118.     EndIf
  119.   Next t
  120. Next l
  121.  
  122. ; Setup structures for c2p conversions.
  123. Mc2pWindow 0,#vwidth,#vheight,dwidth,#bmwidth,#bmheight
  124. MUsec2pWindow 0 ; We use this newly created c2p window
  125.  
  126. Dim bm.l(1) ; We use two bitmaps for double buffering
  127.  
  128. For l=0 To 1
  129.   ; Get some free CHIP memory
  130.   bm(l)=AllocMem(#bmwidth*#bmheight,#MEMF_CHIP)
  131.   If bm(l)   ; and if we succeed
  132.     ; make it a planar bitmap.
  133.     CludgeBitMap l,#bmwidth,#bmheight,8,bm(l)
  134.   Else
  135.     End
  136.   EndIf
  137. Next l
  138.  
  139. ; We need screen too ;)
  140.  
  141. Dim scrtaglst.TagItem(7)            ; All this stuff sets up our
  142. scrtaglst(0)\ti_Tag = #SA_Left      ; Taglist for the screen we
  143. scrtaglst(0)\ti_Data = 0            ; want.
  144. scrtaglst(1)\ti_Tag = #SA_Depth
  145. scrtaglst(1)\ti_Data = 8
  146. scrtaglst(2)\ti_Tag = #SA_Width
  147. scrtaglst(2)\ti_Data = #vwidth
  148. scrtaglst(3)\ti_Tag = #SA_Height
  149. scrtaglst(3)\ti_Data = #vheight
  150. scrtaglst(4)\ti_Tag = #SA_BitMap
  151. scrtaglst(4)\ti_Data = Addr BitMap (0)
  152. scrtaglst(5)\ti_Tag = #SA_ShowTitle
  153. scrtaglst(5)\ti_Data = 0
  154. scrtaglst(6)\ti_Tag = #SA_Draggable
  155. scrtaglst(6)\ti_Data = 0
  156. scrtaglst(7)\ti_Tag = #TAG_END      ; The most important tag of them all.
  157.  
  158. ScreenTags 0,"MildredDEMO",&scrtaglst(0) ; Open our intuition screen.
  159.  
  160. InitPalette 0,255       ; Initialize GrayScalePalette
  161. For l.l=0 To 255
  162.   AGAPalRGB 0,l,l,l,l
  163. Next l
  164.  
  165. ShowPalette 0                       ; Attach our palette to the screen.
  166.  
  167. Dim thrustx.q(#anglesweep-1),thrusty.q(#anglesweep-1)
  168. ; These are used in acceleration
  169. ; Precalculated tables are much faster than using sine and cosine functions
  170. For t.l=0 To #anglesweep-1
  171.   thrustx(t)=-Cos(degrad*t)*0.00027
  172.   thrusty(t)=-Sin(degrad*t)*0.00027
  173. Next t
  174.  
  175. shp.ship\x=#vwidth2,#vheight2   ; Init start position of our ship
  176. shp.ship\velx=0.0,0.0
  177. angle.w=#quarter                ; and the start angle
  178.  
  179. x.q=0                           ; Temporary Coordinate variables
  180. y.q=0
  181. act.w=0                         ; active bitmap number
  182. fuel.l=4000                     ; Amount of fuel we have
  183. mass.l=8000-fuel                ; And our Mass is here (it is reverse)
  184.                                 ; The higher it is the faster the ship will move
  185.  
  186. MGetaShape #anglesweep+3,x,y,16,16 ; save background of the ship
  187.  
  188. Dim bpos.pos(#numbul-1),bvel.vel(#numbul-1)
  189. Dim bul.w(#numbul-1),bg.vel(#numbul-1)
  190. ; Init Bullet variables here
  191. For l=0 To #numbul-1
  192.   bpos(l)\x=0,0
  193.   bvel(l)\vx=0,0
  194.   bul(l)=0
  195.   bg(l)\vx=0,0.045
  196. Next l
  197.  
  198. MParticleFormat -1 ; Data is in x.q,y.q format
  199.  
  200. ; Main Loop starts here!
  201.  
  202. Repeat
  203.   xx.l=Min(Max(0,shp\x-#vwidth2),dwidth-#vwidth)    ; Super Bitmap X position
  204.   yy.l=Min(Max(0,shp\y-#vheight2),dheight-#vheight) ; Super Bitmap Y position
  205.  
  206.   Mc2p 0,MBitmapPtr(xx,yy,0),bm(act)                ; Make C2P to the bitmap
  207.                                                     ; For GFX Board version
  208. ; this have to be replaced with something like writepixelarray_
  209.   WaitTOF_ ; Wait top of frame and...
  210.  
  211.   ShowBitMap act ; show the next frame
  212.  
  213.   MBoxF x,y,x+15,y+15,0         ; Clear the ship area
  214.   MBlit #anglesweep+3,x,y       ; And restore background
  215.   act=1-act                     ; Swap active bitmap
  216.   cangle.w=angle                ; Collision angle (if collision detected
  217.                                 ; angle=cangle)
  218.   ax.w=Joyx(1)                  ; read Joy
  219.   angle=(angle+ax)&(#anglesweep-1) ; and calculate new angle
  220.  
  221.   x=shp\x   ; Init Temporary coordinates
  222.   y=shp\y
  223.  
  224.   If Joyb(1)=1 AND fuel>0               ; Are we thrusting and do we have fuel?
  225.     fuel-1                              ; Sub fuel
  226.     mass+1                              ; and add mass -> will thrust more
  227.     accx.q=(thrustx(angle)*mass)        ; X Acceleration
  228.     accy.q=(thrusty(angle)*mass)+0.35   ; Y Acceleration
  229.     shp\velx+accx-(shp\velx*0.02)       ; X Velocity
  230.     shp\vely+accy-(shp\vely*0.02)       ; Y Velocity
  231.   Else
  232.     shp\velx-(shp\velx*0.02)            ; X Velocity
  233.     shp\vely+(0.35-(shp\vely*0.02))     ; Y Velocity
  234.   EndIf
  235.   x+shp\velx                            ; Add Velocity to Temp Coordinates
  236.   y+shp\vely
  237.  
  238.   sx1.w=x+scx(angle,0)  ; Collision coordinates
  239.   sy1.w=y+scy(angle,0)
  240.   sx2.w=x+scx(angle,1)
  241.   sy2.w=y+scy(angle,1)
  242.   sx3.w=x+scx(angle,2)
  243.   sy3.w=y+scy(angle,2)
  244.  
  245.   mp1.w=MPoint(sx1,sy1)&$ff     ; Read Collision point colours
  246.   mp2.w=MPoint(sx2,sy2)&$ff
  247.   mp3.w=MPoint(sx3,sy3)&$ff
  248.   scol.w=0                      ; Collisions hapened
  249.   slnd.w=0                      ; Landing points true
  250.  
  251.   If mp1>0      ; If the first collision point is larger than 0 then
  252.     scol+1      ; collision true for this point
  253.   EndIf
  254.   If mp2>0      ; If second point is larger than 0 we check
  255.     If mp2=255  ; if the colour is 255
  256.       slnd+1    ; it is so Landing in point 1 is true
  257.     EndIf
  258.     scol+2      ; and collision is also true
  259.   EndIf
  260.   If mp3>0      ; If third point is larger than 0 we check
  261.     If mp3=255  ; if the colour is 255
  262.       slnd+2    ; it is so Landing in point 1 is true
  263.     EndIf
  264.     scol+4      ; and collision is also true
  265.   EndIf
  266.  
  267.   If slnd=3 AND sy2=sy3                         ; If both landing points are true we do landing
  268.     If shp\vely<0.36 AND shp\vely>0
  269.       shp\vely=0 : y=shp\y : shp\velx=0
  270.     Else
  271.       shp\vely=shp\vely*(0.85*Sgn(-shp\vely))
  272.       shp\velx=shp\velx*0.85
  273.     EndIf
  274.     scol-6                                      ; And these points can't collide any more
  275.   EndIf
  276.  
  277.   If scol>0                             ; Is any collisions?
  278.     If scol=7 Then death=#BOOL_TRUE     ; If all points collide we are DEAD!!!
  279.     shp\velx=-shp\velx*0.3              ; Else Our velocity will drop and reverse
  280.     shp\vely=-shp\vely*0.3              ; ie we bounce
  281.     x=shp\x                             ; and our position is not coing to change
  282.     y=shp\y
  283.     angle=cangle                        ; and angle is restored
  284.   EndIf
  285.  
  286.   shp\x=x               ; Put temp position to ship
  287.   shp\y=y
  288.  
  289.   For l=0 To #numbul-1  ; Wipe off all bullets which are
  290.     If bul(l)>0         ; currently wisible
  291.       bul(l)-1          ; and sub it's life
  292.       MPlot bpos(l)\x,bpos(l)\y,0       ; and restore background colour 0
  293.     EndIf
  294.   Next l
  295.  
  296.   If shootdelay.w>0 Then shootdelay-1   ; If we have shot then we have to wait a litle
  297.   If Joyy(1)=-1 AND shootdelay.w=0      ; Are we Shooting?
  298.     shootdelay.w=0                      ; Set delay so that shooting is slower
  299.     curbul+1                            ; Add current bullet by one
  300.     If curbul=#numbul Then curbul=0     ; We chenck if we need to start from the first one
  301.     bul(curbul)=400                     ; Bullets life in frames
  302.     bpos(curbul)\x=x+bx(angle),y+by(angle) ; It's position
  303.     bvel(curbul)\vx=bvx(angle),bvy(angle)  ; and velocity
  304.   EndIf
  305.  
  306.   MAddToParticles &bvel(0)\vx,#numbul,&bg(0)\vx   ; make gravity changes to bullets velocities
  307.   MAddToParticles &bpos(0)\x,#numbul,&bvel(0)\vx  ; add velocity to coords
  308.  
  309.   For l=0 To #numbul-1  ; draw active bullets
  310.     If bul(l)>0
  311.       bc.w=MPoint(bpos(l)\x,bpos(l)\y)&$ff
  312.       If bc=0 OR bc=255
  313.         MPlot bpos(l)\x,bpos(l)\y,255
  314.       Else
  315.         bul(l)=0
  316.       EndIf
  317.     EndIf
  318.   Next l
  319.  
  320.   MGetaShape #anglesweep+3,x,y,16,16    ; save background
  321.   MBlit angle+3,x,y                     ; and blit the ship
  322.  
  323. Until RawStatus($45) OR death           ; until we "escape" or death
  324.  
  325. End                                     ; End our nice program.;)
  326.